package com.example.dell.fastshopping.order.model;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.order.OrderInfo;
import com.google.gson.GsonBuilder;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class OderInfoViewMolder extends ViewModel {
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<BaseResponse> updateAddress = new MutableLiveData<>();
    private MutableLiveData<OrderInfo> orderInfo = new MutableLiveData<>();

    public MutableLiveData<BaseResponse> updateAddress(String address, int id, String phone) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("orderAddress", address);
        map.put("id", id);
        map.put("phone", phone);
        String s = new GsonBuilder().create().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), s);

        RetrofitClient.execute(service.updateAddress(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse baseResponse) {
                updateAddress.setValue(baseResponse);
            }
        });
        return updateAddress;
    }

    public MutableLiveData<OrderInfo> getOrderDetial(int id) {
        RetrofitClient.execute(service.getOrderDetial(id), new NetCallback<BaseResponse<OrderInfo>>() {
            @Override
            public void netCallback(BaseResponse<OrderInfo> o) {
                orderInfo.setValue(o.getData());
            }
        });
        return orderInfo;
    }
}
